package com.example.database_manage.admin;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.database_manage.R;
import com.example.database_manage.database.CommonDatabase;
import com.example.database_manage.student.about_me;


/***
 * 这里主要是点击listview中的学生信息时，对应的相应activity
 */
public class delete_change extends AppCompatActivity implements View.OnClickListener {
    EditText edit_id_d;
    EditText edit_name_d;
    EditText edit_class_d;
    EditText edit_sex_d;
    EditText edit_age_d;
    EditText edit_birth_d;
    EditText edit_clubInfo_d;
    EditText edit_awards_d;
    EditText edit_work_d;
    String  t_d_id ;
    String  t_d_name ;
    String  t_d_class ;
    SQLiteDatabase db;
    //false代表没点删除，点的是
    boolean flag = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_change);
        //获取数据库对象
        db = new CommonDatabase().getSqliteObject(delete_change.this, "test_db");
        //按钮绑定
        Button button_delete = findViewById(R.id.button_delete);

        Button button_baocun = findViewById(R.id.button_baocun);
        //监听器
        button_delete.setOnClickListener(this);
        button_baocun.setOnClickListener(this);
        //编辑框绑定
        edit_id_d = findViewById(R.id.edit_id_d);
        edit_name_d = findViewById(R.id.edit_name_d);
        edit_sex_d = findViewById(R.id.edit_sex_d);
        edit_age_d = findViewById(R.id.edit_age_d);
        edit_class_d = findViewById(R.id.edit_class_d);
        edit_birth_d = findViewById(R.id.edit_birth_d);
        edit_clubInfo_d = findViewById(R.id.edit_clubInfo_d);
        edit_awards_d = findViewById(R.id.edit_awards_d);
        edit_work_d = findViewById(R.id.edit_work_d);

        Intent intent_receive = getIntent();

//        edit_id_d.setText(intent_receive.getStringExtra("id"));

        @SuppressLint("Recycle") Cursor cursor_about = db.query("student", null, "id = ?", new String[]{intent_receive.getStringExtra("id")}, null, null, null);
        while (cursor_about.moveToNext()) {
            //将通过id查询到的学生信息显示到界面中
            edit_id_d.setText(cursor_about.getString(cursor_about.getColumnIndex("id")));
            edit_name_d.setText(cursor_about.getString(cursor_about.getColumnIndex("sname")));
            edit_sex_d.setText(cursor_about.getString(cursor_about.getColumnIndex("sex")));
            edit_age_d.setText(cursor_about.getString(cursor_about.getColumnIndex("age")));
            edit_class_d.setText(cursor_about.getString(cursor_about.getColumnIndex("classroom")));
            edit_birth_d.setText(cursor_about.getString(cursor_about.getColumnIndex("birth")));
            edit_clubInfo_d.setText(cursor_about.getString(cursor_about.getColumnIndex("clubInfo")));
            edit_awards_d.setText(cursor_about.getString(cursor_about.getColumnIndex("awards")));
            edit_work_d.setText(cursor_about.getString(cursor_about.getColumnIndex("graduationWork")));


        }

//        //把intent中携带的信息设置成为编辑框的初始内容，方便修改
//        edit_id_d.setText(intent_receive.getStringExtra("id"));
//        edit_name_d.setText(intent_receive.getStringExtra("sname"));
//        edit_class_d.setText(intent_receive.getStringExtra("classroom"));
//        edit_sex_d.setText(intent_receive.getStringExtra("sex"));
//        edit_age_d.setText(intent_receive.getStringExtra("age"));
//        edit_birth_d.setText(intent_receive.getStringExtra("birth"));
//        edit_clubInfo_d.setText(intent_receive.getStringExtra("clubInfo"));
//        edit_awards_d.setText(intent_receive.getStringExtra("awards"));
//        edit_work_d.setText(intent_receive.getStringExtra("graduationWork"));
//



        //获取初始值
        t_d_id = edit_id_d.getText().toString();
        t_d_name = edit_name_d.getText().toString();
        t_d_class = edit_class_d.getText().toString();

    }

    @Override
    public void onClick(View v) {
        CommonDatabase commonDatabase = new CommonDatabase();
        final SQLiteDatabase db = commonDatabase.getSqliteObject(delete_change.this,"test_db");

        switch (v.getId())
        {
            case R.id.button_delete:
                //    通过AlertDialog.Builder这个类来实例化我们的一个AlertDialog的对象
                AlertDialog.Builder builder = new AlertDialog.Builder(delete_change.this);
                //    设置Title的图标
                builder.setIcon(R.drawable.ic_launcher_background);
                //    设置Title的内容
                builder.setTitle("弹出警告框");
                //    设置Content来显示一个信息
                builder.setMessage("确定删除吗？");
                //    设置一个PositiveButton
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        db.delete("student","id=?",new String[]{t_d_id});
                        finish();
                    }
                });
                //    设置一个NegativeButton
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {

                    }
                });

                //    显示出该对话框
                builder.show();

                break;
                /****更改*****/
            case R.id.button_baocun:


                //获取这三项数据，并写入contentvalues中
                ContentValues values2 = new ContentValues();
                values2.put("sname", edit_name_d.getText().toString());
                values2.put("id", edit_id_d.getText().toString());
                values2.put("classroom", edit_class_d.getText().toString());
                values2.put("sex", edit_sex_d.getText().toString());
                values2.put("age", edit_age_d.getText().toString());
                values2.put("birth", edit_birth_d.getText().toString());
                values2.put("clubInfo", edit_clubInfo_d.getText().toString());
                values2.put("awards", edit_awards_d.getText().toString());
                values2.put("graduationWork", edit_work_d.getText().toString());

                db.update("student", values2, "id = ? ", new String[]{t_d_id});
                finish();
                break;

            default:
                break;




        }
    }


}
